package com.cl.code.common.utils;

import java.io.*;

/**
 * @author chengliang
 * @since 1.0.0
 */
public class FileUtils {

    /**
     * 读取文件
     *
     * @param pathname 文件路径
     * @return 文件
     */
    public static File readFile(String pathname) {
        return new File(pathname);
    }

    /**
     * 读取文件内容
     *
     * @param pathname 文件路径
     * @return 文件内容
     * @throws IOException 读取异常
     */
    public static String readFileContent(String pathname) throws IOException {
        if (pathname == null) {
            throw new IOException("pathname is null");
        } else {
            return readFileContent(new File(pathname));
        }
    }

    /**
     * 读取文件内容
     *
     * @param file 文件
     * @return 文件内容
     * @throws IOException 读取异常
     */
    public static String readFileContent(File file) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            return readFileContent(fis);
        }
    }

    /**
     * 读取文件内容
     *
     * @param is 输入流
     * @return 文件内容
     * @throws IOException 读取异常
     */
    public static String readFileContent(InputStream is) throws IOException {
        try (BufferedInputStream bis = new BufferedInputStream(is)) {
            StringBuilder sb = new StringBuilder();
            byte[] bytes = new byte[1024];
            int read;
            while ((read = bis.read(bytes)) != -1) {
                sb.append(new String(bytes, 0, read));
            }
            return sb.toString();
        }
    }

    /**
     * 写文件
     *
     * @param pathname 文件路径
     * @param content  文本内容
     * @throws IOException 写入异常
     */
    public static void writeFile(String pathname, String content) throws IOException {
        if (content == null) {
            throw new IOException("content is null");
        }
        try (ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes())) {
            writeFile(pathname, is);
        }
    }

    /**
     * 写文件
     *
     * @param pathname 文件路径
     * @param file     文件
     * @throws IOException 写入异常
     */
    public static void writeFile(String pathname, File file) throws IOException {
        try (FileInputStream fis = new FileInputStream(file)) {
            writeFile(pathname, fis);
        }
    }

    /**
     * 写文件
     *
     * @param pathname 文件路径
     * @param is       输入流
     * @throws IOException 写入异常
     */
    public static void writeFile(String pathname, InputStream is) throws IOException {
        File newFile = readFile(pathname);
        if (newFile.exists()) {
            throw new IOException("output file already exists");
        } else {
            File directory = newFile.getParentFile();
            if (directory != null) {
                // create directory if not exists
                if (!directory.exists()) {
                    boolean createDirectorySuccess = directory.mkdirs();
                    if (!createDirectorySuccess) {
                        throw new IOException("create output file directory failed");
                    }
                } else if (!directory.isDirectory()) {
                    throw new IOException("output file directory error");
                }
            }
            boolean createOutputFileSuccess = newFile.createNewFile();
            if (!createOutputFileSuccess) {
                throw new IOException("create output file failed");
            } else {
                try (BufferedInputStream bis = new BufferedInputStream(is)) {
                    try (FileOutputStream fos = new FileOutputStream(newFile)) {
                        try (BufferedOutputStream bos = new BufferedOutputStream(fos)) {
                            byte[] bytes = new byte[1024];
                            int read;
                            while ((read = bis.read(bytes)) != -1) {
                                bos.write(bytes, 0, read);
                            }
                        }
                    }
                }
            }
        }
    }

}
